//------------------------------------------------------------------------------
// Copyright (c) 2008 www.dnaide.com
// Licensed under the MIT (MIT-LICENSE.txt)
//------------------------------------------------------------------------------
using System;
using System.Drawing;
using System.Drawing.Imaging;
using DNAide.Core;

namespace DNAide.Drawing
{

	public static partial class BitmapAide
	{

		/// <summary>
		/// Draws a bitmap onto a canvas bitmap
		/// </summary>
		/// <param name="canvasBitmap"></param>
		/// <param name="bitmapToDraw"></param>
		/// <param name="drawPosition"></param>
		public static void DrawOnToCanvasBitmap(ref Bitmap canvasBitmap, Bitmap bitmapToDraw, DrawPosition drawPosition)
		{
			DrawOnToCanvasBitmap(ref canvasBitmap, bitmapToDraw, drawPosition, null);
		}


		/// <summary>
		/// Draws a bitmap onto a canvas bitmap
		/// </summary>
		/// <param name="canvasBitmap"></param>
		/// <param name="bitmapToDraw"></param>
		/// <param name="drawPosition"></param>
		/// <param name="graphicsSettings"></param>
		public static void DrawOnToCanvasBitmap(ref Bitmap canvasBitmap, Bitmap bitmapToDraw, DrawPosition drawPosition, GraphicsSettings graphicsSettings)
		{
			DrawOnToCanvasBitmap(ref canvasBitmap, bitmapToDraw, drawPosition, graphicsSettings, true);
		}


		/// <summary>
		/// Draws a bitmap onto a canvas bitmap
		/// </summary>
		/// <param name="canvasBitmap"></param>
		/// <param name="bitmapToDraw"></param>
		/// <param name="drawPosition"></param>
		/// <param name="graphicsSettings"></param>
		/// <param name="scaleToFitCanvas"></param>
		public static void DrawOnToCanvasBitmap(ref Bitmap canvasBitmap, Bitmap bitmapToDraw, DrawPosition drawPosition, GraphicsSettings graphicsSettings, Boolean scaleToFitCanvas)
		{
			Point point = new Point();
			point.X = 0;
			point.Y = 0;

			Bitmap finalBitmapToDraw = BitmapAide.CopyBitmap(bitmapToDraw);
			if (scaleToFitCanvas && (bitmapToDraw.Width > canvasBitmap.Width || bitmapToDraw.Height > canvasBitmap.Height))
			{
				BitmapAide.Resize(ref finalBitmapToDraw, canvasBitmap.Width, canvasBitmap.Height, true, graphicsSettings);
			}
			
			Bitmap mergedBitmap = new Bitmap(canvasBitmap.Width, canvasBitmap.Height);
			using (Graphics g = Graphics.FromImage((Image)mergedBitmap))
			{
				if (graphicsSettings != null)
				{
					graphicsSettings.ApplySettings(g);
				}
				g.DrawImage(canvasBitmap, 0, 0, canvasBitmap.Width, canvasBitmap.Height);

				string drawPositionStr = drawPosition.ToString();
				if (drawPositionStr.StartsWith("Bottom"))
				{
					point.Y = canvasBitmap.Height - finalBitmapToDraw.Height;
				}
				else if (drawPositionStr.StartsWith("Center"))
				{
					point.Y = CastAide.AsInt32((double)(canvasBitmap.Height - finalBitmapToDraw.Height) / 2);
				}

				if (drawPositionStr.EndsWith("Right"))
				{
					point.X = canvasBitmap.Width - finalBitmapToDraw.Width;
				}
				else if (drawPositionStr.EndsWith("Center"))
				{
					point.X = CastAide.AsInt32((double)(canvasBitmap.Width - finalBitmapToDraw.Width) / 2);
				}

				g.DrawImage(finalBitmapToDraw, point.X, point.Y, finalBitmapToDraw.Width, finalBitmapToDraw.Height);
			}

			finalBitmapToDraw.Dispose(); finalBitmapToDraw = null;
			canvasBitmap.Dispose(); canvasBitmap = null;
			canvasBitmap = BitmapAide.CopyBitmap(mergedBitmap);
			mergedBitmap.Dispose(); mergedBitmap = null;
		}


		/// <summary>
		/// Draws a bitmap onto a canvas bitmap
		/// </summary>
		/// <param name="canvasBitmap"></param>
		/// <param name="bitmapToDraw"></param>
		/// <param name="point"></param>
		public static void DrawOnToCanvasBitmap(Bitmap canvasBitmap, Bitmap bitmapToDraw, Point point)
		{
			DrawOnToCanvasBitmap(ref canvasBitmap, bitmapToDraw, point, null, true);
		}


		/// <summary>
		/// Draws a bitmap onto a canvas bitmap
		/// </summary>
		/// <param name="canvasBitmap"></param>
		/// <param name="bitmapToDraw"></param>
		/// <param name="point"></param>
		/// <param name="graphicsSettings"></param>
		public static void DrawOnToCanvasBitmap(Bitmap canvasBitmap, Bitmap bitmapToDraw, Point point, GraphicsSettings graphicsSettings)
		{
			DrawOnToCanvasBitmap(ref canvasBitmap, bitmapToDraw, point, graphicsSettings, true);
		}


		/// <summary>
		/// Draws a bitmap onto a canvas bitmap
		/// </summary>
		/// <param name="canvasBitmap"></param>
		/// <param name="bitmapToDraw"></param>
		/// <param name="point"></param>
		/// <param name="graphicsSettings"></param>
		/// <param name="scaleToFitCanvas"></param>
		public static void DrawOnToCanvasBitmap(ref Bitmap canvasBitmap, Bitmap bitmapToDraw, Point point, GraphicsSettings graphicsSettings, Boolean scaleToFitCanvas)
		{
			Bitmap finalBitmapToDraw = BitmapAide.CopyBitmap(bitmapToDraw);
			if (scaleToFitCanvas && (bitmapToDraw.Width > canvasBitmap.Width || bitmapToDraw.Height > canvasBitmap.Height))
			{
				BitmapAide.Resize(ref finalBitmapToDraw, canvasBitmap.Width, canvasBitmap.Height, true, graphicsSettings);
			}
			
			Bitmap mergedBitmap = new Bitmap(canvasBitmap.Width, canvasBitmap.Height);
			using (Graphics g = Graphics.FromImage((Image)mergedBitmap))
			{
				if (graphicsSettings != null)
				{
					graphicsSettings.ApplySettings(g);
				}
				g.DrawImage(canvasBitmap, 0, 0, canvasBitmap.Width, canvasBitmap.Height);
				g.DrawImage(finalBitmapToDraw, point.X, point.Y, finalBitmapToDraw.Width, finalBitmapToDraw.Height);
			}

			finalBitmapToDraw.Dispose(); finalBitmapToDraw = null;
			canvasBitmap.Dispose(); canvasBitmap = null;
			canvasBitmap = BitmapAide.CopyBitmap(mergedBitmap);
			mergedBitmap.Dispose(); mergedBitmap = null;
		}

	}

}
